<?php
/**
 * Аутентификация пользователей
 */
class UserIdentity extends CUserIdentity
{
	private $_id;
	
	private $_name;
	
	private $_lastname;
	
	private $_email;
	
	public function authenticate()
	{
		$result = User::model()->findByAttributes(array('email' => $this->username));
		
		if($result === null)
		{
			$this->errorCode = self::ERROR_USERNAME_INVALID;
		}
		else if($result->password !== crypt($this->password, $result->password))
		{
			$this->errorCode = self::ERROR_PASSWORD_INVALID;
		}
		else
		{
			$this->_id = $result->ID;
			$this->_name = $result->name;
			$this->_lastname = $result->lastname;
			$this->_email = $result->email;
			$this->errorCode = self::ERROR_NONE;
		}
		return !$this->errorCode;
	}
	
	public function getId()
	{
		return $this->_id;
	}
	
	public function getName()
	{
		if(empty($this->_name) && empty($this->_lastname))
		{
			return $this->_email;
		}
		elseif(empty($this->_name))
		{
			return $this->_lastname;
		}
		else
		{
			return $this->_name . ' ' . $this->_lastname;
		}
	}
}